package com.neu.service;


import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.common.JwtTokenUtils;
import com.neu.dao.AdminDao;
import com.neu.dao.ApplyDao;
import com.neu.dao.HireDao;
import com.neu.entity.Admin;
import com.neu.entity.Apply;
import com.neu.entity.Hire;
import com.neu.entity.Params;
import com.neu.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApplyService {
    @Autowired
    private ApplyDao applyDao;

    @Autowired
    private AdminDao adminDao;

//    @Autowired
//    private AdminDao adminDao;

    public PageInfo<Apply> findBySearch(Params params) {
        Admin user = JwtTokenUtils.getCurrentUser();
        if(ObjectUtil.isNull(user)){
            throw new CustomException("从token中未解析到用户信息，请重新登录");
        }
        if("ROLE_ADMIN".equals(user.getRole())){
            params.setAuditId(user.getId());
        }else{
            params.setApplyId(user.getId());
        }
//        if("ROLE_TUTOR".equals(user.getRole())){
//            params.setTutorId(user.getId());
//        }
        //开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        //接下来的查询会自动按照当前开启的分页设置来查询
        //List<Hire> list = hireDao.findBySearch(params);
        List<Apply> list = applyDao.findBySearch(params);
//        if(CollectionUtil.isEmpty(list)){
//            return PageInfo.of(new ArrayList<>());
//        }
//        for(Hire hire:list){
//            if(ObjectUtil.isNotEmpty(hire.getTutorId())){
//                Admin admin = adminDao.selectByPrimaryKey(hire.getTutorId());
//                if(ObjectUtil.isNotEmpty(admin)){
//                    hire.setTutorName(admin.getName());
//                    hire.setPrice(admin.getPrice());
//                }
//            }
//            if(ObjectUtil.isNotEmpty(hire.getUserId())){
//                Admin admin = adminDao.selectByPrimaryKey(hire.getUserId());
//                if(ObjectUtil.isNotEmpty(admin)){
//                    hire.setUserName(admin.getName());
//                }
//
//            }
//        }
        return PageInfo.of(list);
    }

    public void add(Apply apply) {
        applyDao.insertSelective(apply);
    }

    public void update(Apply apply) {
        applyDao.updateByPrimaryKeySelective(apply);
    }

    public void delete(Integer id) {
        applyDao.deleteByPrimaryKey(id);
    }

    public void audit(Apply apply) {
        applyDao.updateByPrimaryKeySelective(apply);
        
    }

    public PageInfo<Apply> findByAll(Params params) {

        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        //接下来的查询会自动按照当前开启的分页设置来查询
        //List<Hire> list = hireDao.findBySearch(params);
//        List<Apply> list = applyDao.findByAll(params);
        List<Apply> list = applyDao.findByAll(params);
        return PageInfo.of(list);
    }




}
